package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lifecycle.domain.LifManageList;
import com.ruoyi.lifecycle.service.ILifManageListService;
import com.ruoyi.system.domain.SysInformation;
import com.ruoyi.system.service.ISysInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*项目信息管理
* @Author zhaoguanyu
* @Date 13:59 2021/11/29
*/
@Api(tags = "项目信息管理")
@RestController
@RequestMapping("/system/information")
public class SysInformationController extends BaseController {

    @Autowired
    ISysInformationService sysInformationService;

    @Autowired
    ILifManageListService lifManageListService;

    /*
    * 获取项目列表*/
    @ApiOperation(value = "获取项目列表")
    @PreAuthorize("@ss.hasPermi('document:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysInformation sysInformation){
        startPage();
        List<SysInformation> list = sysInformationService.selectPostList(sysInformation);
        return getDataTable(list);
    }

    /*
     * 根据项目经理查询项目*/
    @ApiOperation(value = "根据项目经理查询项目")
    @PreAuthorize("@ss.hasPermi('document:information:projectorList')")
    @GetMapping("/projectorList")
    public TableDataInfo projectorList(){
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<SysInformation> list = sysInformationService.selectProjectorList(userId);
        return getDataTable(list);
    }


    /*
    *项目添加
    *  */
    @ApiOperation(value = "项目添加")
    @PreAuthorize("@ss.hasPermi('document:information:add')")
    @Log(title = "项目信息管理",businessType = BusinessType.INSERT)
    @PostMapping("/sysAdd")
    public AjaxResult add(@Validated @RequestBody SysInformation sysInformation){
        if (UserConstants.NOT_UNIQUE.equals(sysInformationService.checkItemNameUnique(sysInformation))){
            return AjaxResult.error("新增项目" + sysInformation.getItemName() + "失败,该项目名称已存在");
        }else if (UserConstants.NOT_UNIQUE.equals(sysInformationService.checkItemNoUnique(sysInformation))){
            return AjaxResult.error("新增项目" + sysInformation.getItemNo() + "失败，该项目编码已存在");
        }

        sysInformation.setCreateBy(getUsername());
        return toAjax(sysInformationService.insertInformation(sysInformation));
    }

    /**
     * 项目修改
     */
    @ApiOperation(value = "项目修改")
    @PreAuthorize("@ss.hasPermi('document:information:edit')")
    @Log(title = "项目信息管理",businessType = BusinessType.UPDATE)
    @PutMapping("/sysUpdate")
    public AjaxResult edit(@Validated @RequestBody SysInformation sysInformation) {
        sysInformation.setUpdateBy(getUsername());
        return toAjax(sysInformationService.updateInformation(sysInformation));
    }

    /**
     * 查看工艺设备列表
     */
    @ApiOperation(value = "查看工艺设备列表")
    @PreAuthorize("@ss.hasPermi('document:information:querydevice')")
    @GetMapping("/deviceList")
    public AjaxResult deviceList(SysInformation sysInformation){
        Long projectId = sysInformation.getId();
        Map<String, Object> map = new HashMap<>();
        map.put("sysInformation",sysInformation);
        startPage();
        List<LifManageList> lifManageLists = lifManageListService.selectDeviceList(projectId);
        TableDataInfo dataTable = getDataTable(lifManageLists);
        map.put("dataTable",dataTable);
        return AjaxResult.success(map);
    }

    /**
     * 项目删除
     */
    @ApiOperation(value = "项目删除")
    @PreAuthorize("@ss.hasPermi('document:information:delete')")
    @Log(title = "项目信息管理",businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable  Long[] id) {
        return toAjax(sysInformationService.deleteSys(id));
    }
}
